﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeatEmUp.Enums;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.IO;
using System.Diagnostics;
using Microsoft.Xna.Framework.Input;

namespace BeatEmUp.Items.Weapons
{
    class WeaponsManager
    {
        public List<Weapon> weapons
        {
            private set;
            get;
        }

        public static string[] weaponNames = new string[10] {" of Justice", " of Doom", " of War", " of Death"," of Power", " of Luck",
                            " of Truth"," of Mastery"," of Honour", " of Duty"};
        Dictionary<string,Texture2D>weaponSprites;
        Random rand;
        WeaponPickUp pickUp;

        public WeaponsManager(ContentManager content)
        {
            rand = new Random();
            weapons = new List<Weapon>();
            weaponSprites = new Dictionary<string, Texture2D>();
            pickUp = new WeaponPickUp(Rectangle.Empty);
            pickUp.LoadContent(content);
            LoadWeapons(content);
        }

        private void LoadWeapons(ContentManager content)
        {
            weaponSprites = HelpMethods.LoadContentFromFolder<Texture2D>(content, @"Sprites\Weapons");
        }

        public void GenerateNewWeapon(int monsterLevel, MonsterType monsterType, Rectangle monsterPosition,Weapon playerWeapon)//Generowanie statystyk broni
        {
            string weaponType = "";
            string textureName = "";
            Texture2D texture = GetWeaponTexture(ref weaponType,ref textureName);
            weapons.Add(new Weapon(monsterLevel,monsterType, GenerateWeaponName(weaponType),textureName,texture, 1,new Vector2(monsterPosition.X+monsterPosition.Width/2-texture.Width/2,monsterPosition.Y+monsterPosition.Height - texture.Height) , WeaponLocation.OnGameField));
            CompareLastWeaponValue(playerWeapon);
        }

        private string GenerateWeaponName(string weaponType)
        {
            return weaponType + weaponNames[rand.Next(10)];
        }

        private WeaponTypes RandomType()
        {
            Array values = Enum.GetValues(typeof(WeaponTypes));
            Random random = new Random();
            return (WeaponTypes)values.GetValue(random.Next(values.Length));
        }

        private Texture2D GetWeaponTexture(ref string weaponType,ref string textureName)
        {
            WeaponTypes type = RandomType();
            weaponType = type.ToString();
            List<Texture2D> temp = new List<Texture2D>();
            foreach (string key in weaponSprites.Keys)
                if(key.Contains(type.ToString()))temp.Add(weaponSprites[key]);
            int random = rand.Next(temp.Count);
            int i = 0;
            foreach (string key in weaponSprites.Keys)
                if (key.Contains(type.ToString()))
                {
                    if (i == random)
                    {
                        textureName = key;
                        Debug.WriteLine("Texture name " + textureName);
                    }
                    i++;
                }
            return temp[random];
        }

        public void CompareLastWeaponValue(Weapon playerWeapon)
        {
            if (weapons[weapons.Count - 1].Value > playerWeapon.Value)
            {
                weapons[weapons.Count - 1].CompareToPlayer = 2;
                Debug.WriteLine("Weapon is better than player weapon.");
            }
            if (weapons[weapons.Count - 1].Value == playerWeapon.Value)
            {
                weapons[weapons.Count - 1].CompareToPlayer = 1;
                Debug.WriteLine("Weapon has the same value as player weapon.");
            }
            if (weapons[weapons.Count - 1].Value < playerWeapon.Value)
            {
                weapons[weapons.Count - 1].CompareToPlayer = 0;
                Debug.WriteLine("Weapon is worse than player weapon.");
            }
        }

        public void CompareWithChangedPlayerWeapon(Weapon playerWeapon)
        {
            foreach (Weapon weapon in weapons)
            {
                if (weapon.Value > playerWeapon.Value)
                {
                    weapon.CompareToPlayer = 2;
                    Debug.WriteLine("Weapon is better than player weapon.");
                }
                if (weapon.Value == playerWeapon.Value)
                {
                    weapon.CompareToPlayer = 1;
                    Debug.WriteLine("Weapon has the same value as player weapon.");
                }
                if (weapon.Value < playerWeapon.Value)
                {
                    weapon.CompareToPlayer = 0;
                    Debug.WriteLine("Weapon is worse than player weapon.");
                }
            }
        }

        public void Update(Rectangle playerCollision, KeyboardState keyboard,KeyboardState previous, PlayerWeapon playerWeapon)//Methods to check if there is collision between player and weapon to display comparision and option to pick it up.
        {
            bool pickUpVisible = false;
            for (int i = 0; i < weapons.Count; i++)
            {
                if (weapons[i].Position.Intersects(playerCollision))
                {
                    pickUpVisible = true;
                    pickUp.SetData(new Vector2(weapons[i].Position.X + weapons[i].Width / 2, weapons[i].Position.Y - 5), weapons[i]);
                    if (pickUp.IsVisible && ((keyboard.IsKeyDown(Keys.E)&&previous.IsKeyUp(Keys.E))||(keyboard.IsKeyDown(Keys.Z)&&previous.IsKeyUp(Keys.Z))))
                    {
                        pickUp.IsVisible = false;
                        if (CompareWeapons.IsVisible)
                        {
                            CompareWeapons.IsVisible = false;
                            playerWeapon.Weapon = pickUp.RelatedWeapon;
                            weapons.RemoveAt(i);
                            CompareWithChangedPlayerWeapon(playerWeapon.Weapon);
                        }else
                            CompareWeapons.SetData(new Vector2(weapons[i].Position.X + weapons[i].Width / 2, weapons[i].Position.Y - 5), playerWeapon.Weapon, weapons[i]);
                    }
                    break;
                }
            }
            pickUp.IsVisible = pickUpVisible;
            if (CompareWeapons.IsVisible && pickUpVisible == false)
            {
                CompareWeapons.IsVisible = false;
            }
        }

        public void Draw(SpriteBatch sb)//wyrysowanie wszystkich broni w liście
        {
            foreach (Weapon weapon in weapons)
            {
                weapon.Draw(sb);
            }
            pickUp.Draw(sb);
            
        }
    }
}
